package com.permission.controller;

import com.permission.model.PDept;
import com.permission.service.PDeptService;
import com.permission.util.BizConstants;
import com.solution.framework.controller.BaseController;
import com.solution.framework.util.*;
import org.hibernate.criterion.Criterion;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created with generator tools.
* User: sunguangzhu
* DateTime:2014-04-28 04:08:30
*/
@Controller
public class PDeptController extends BaseController<PDept> {

private static final String path_index = "pDept/pDept_index";
private static final String path_input = "pDept/pDept_input";
private static final String path_view = "pDept/pDept_view";

@Resource
private PDeptService pDeptService;

@RequestMapping
public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("pDept index method ....");
        Pagination pagination = RequestVarsUtil.getPagination(request);
        List<Criterion> criterions = this.getSearchCriterions(request);
        List<PDept> entitys = this.pDeptService.listByCriteria(pagination, criterions);
        int total = this.pDeptService.getTotalByCriteria(criterions);
        RequestVarsUtil.setPagination(pagination, total);
        request.setAttribute("pageModel", pagination);
        model.addAttribute("list", entitys);
        return path_index;
}

    @RequestMapping
    public String edit(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("pDept edit method ....");
        PDept pDept = this.pDeptService.get(id);
        if(pDept.getParent()==null){
            PDept parent=new PDept();
            parent.setId(0l);
            pDept.setParent(parent);
        }
        model.addAttribute("pDept", pDept);
        return path_input;
    }

    @RequestMapping
    public String add(Long bizId, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("pDept add method ....");
        PDept pDept = new PDept();
        PDept parent=null;
        if(bizId!=0l){
            parent = this.pDeptService.get(bizId);
        }else{
            parent= new PDept();
            parent.setId(0l);
        }
        pDept.setParent(parent);
        model.addAttribute("pDept", pDept);
        return path_input;
    }

    @RequestMapping
    public String addInDialog(Long bizId, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("pDept add method ....");
        PDept pDept = new PDept();
        PDept parent=null;
        if(bizId!=0l){
            parent = this.pDeptService.get(bizId);
        }else{
            parent= new PDept();
            parent.setId(0l);
        }
        pDept.setParent(parent);
        model.addAttribute("pDept", pDept);
        return path_input;
    }

    @RequestMapping
    public String view(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("pDept view method ....");
        PDept pDept = this.pDeptService.get(id);
        model.addAttribute("pDept", pDept);
        return path_view;
    }

    @ResponseBody
    @RequestMapping
    @DateTimeFormat
    public void save(PDept pDept, HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    System.out.println("pDept save method ....");
    if(pDept.getParent().getId().longValue()==0l){
        pDept.setParent(null);
    }
    MessageDto messageDto = new MessageDto();
    try {
    if (pDept.getId()!=null) {
    this.pDeptService.update(pDept);
    super.setMessageDto(messageDto, MessageDefined.update_success_info);
    } else {
    this.pDeptService.save(pDept);
    super.setMessageDto(messageDto, MessageDefined.save_success_info);
    }
    } catch (Exception e) {
    messageDto.setSuccess(false);
    super.setMessageDto(messageDto, MessageDefined.sys_error_info);
    e.printStackTrace();
    }
    super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void delete(String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
    MessageDto messageDto = new MessageDto();
    try {
    Integer count = this.pDeptService.delete(ids);
    super.setMessageDto(messageDto, MessageDefined.del_success_info);
    } catch (Exception e) {
    messageDto.setSuccess(false);
    super.setMessageDto(messageDto, MessageDefined.sys_error_info);
    e.printStackTrace();
    }
    super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void tree(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<PDept> entitys = this.pDeptService.listByHql();
        List<TreeModel> treeModels = new ArrayList<TreeModel>();
        TreeUtils.addTreeRoot(treeModels, BizConstants.PDEPT_TREE);
        for (PDept entity : entitys) {
            TreeModel treeModel = new TreeModel();
            treeModel.setId(entity.getId());
            treeModel.setPid(entity.getParent() == null ? 0l : entity.getParent().getId());
            treeModel.setText(entity.getName());
            treeModels.add(treeModel);
        }
        treeModels = TreeUtils.generateTree(treeModels);
        String results = JsonUtil.transformObjectToString(treeModels);
        super.writeToClient(response, results);
    }
}
